package com.ulife.permission.module.screen;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.dao.dataobject.UserDO;
import com.ulife.permission.manager.UserManager;

public class UlifeController {

	private static final Logger log = LoggerFactory.getLogger(RoleList.class);

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private UserManager userManager;
	
	public void execute(Context context, Navigator nav) {
		try {
			String operate = request.getParameter("operate");
			if (StringUtils.equals(operate, "initAdmin")) {
				initAdmin();
			}
		} catch (ManagerException e) {
			context.put("error", "系统错误");
			nav.forwardTo("error");
			log.error("get user list error",e);
		}
	}

	private void initAdmin() throws ManagerException {
		UserDO user = new UserDO();
		user.setUserName("admin");
		user.setPassword("system");
		user.setRealName("管理员");
		user.setRoleIdList("admin");
		user.setStatus(0);
		Result<UserDO> result = userManager.addUser(user);
		if (result == null || !result.isSuccess()) {
			log.error("init admin error");
		}
	}
}
